<?php
session_start ();

// ~ print_r($_POST);
// ~ print_r($_SESSION);

$date = getdate ();
$_SESSION ['date'] = $date ['year'] . '-' . $date ['mon'] . '-' . $date ['mday'];

if (! isset ( $_SESSION ['id'] )) {
	header ( 'Location: http://localhost/SPATAX_beta/index.php' );
} else {
	
	$_SESSION ['pathway'] = '.';
	$_SESSION ['pathway_js'] = '';
	$_SESSION ['authentification'] = '1';
	require_once ("./requetes/last_login.php");
	require_once ("./commun/ouvre_base.php");
	include ("./header.php");
	
	require_once ("./requetes/recup_niveau.php");
	
	if (isset ( $_POST ['formclinique'] ) and $_POST ['formclinique'] != '') {
		
		$verifie_existence_form = "SELECT COUNT(*) > 0 FROM `$db`.`diagnostic` WHERE `form`=" . $_POST ['formclinique'];
		$resultat_verifie_existence = mysql_query ( $verifie_existence_form );
		// ~ $resultat_verifie_existence = mysqli_query($link,$verifie_existence_form);
		$ligne = mysql_fetch_row ( $resultat_verifie_existence );
		// ~ $ligne = mysqli_fetch_row($resultat_verifie_existence);
		$res = $ligne [0];
		
		if ($res == 1) {
			$get_form = "SELECT * FROM `$db`.`diagnostic` WHERE `form`=" . $_POST ['formclinique'];
			$resultat_formclinique = mysql_query ( $get_form );
			// ~ $resultat_formclinique = mysqli_query($get_form);
			$ligne = mysql_fetch_row ( $resultat_formclinique );
			// ~ $ligne = mysqli_fetch_row($resultat_formclinique);
			echo "<script>window.location.href=\"./clinique/page_principale.php?form=" . $_POST ['formclinique'] . "\"</script>";
		} elseif ($res == 0) {
			
			?>
<table class='principale'>
	<tr>
		<td>form <?php echo $_POST['formclinique']; ?> not found in diagnostic</td>
	</tr>
</table><?php
		}
	} else if (isset ( $_POST ['nip'] ) and $_POST ['nip'] != '') {
		
		// recherche par nip sur diagnostic
		$verifie_existence_form = "SELECT COUNT(*) > 0 FROM `$db`.`diagnostic` WHERE `nip`=" . $_POST ['nip'];
		$resultat_verifie_existence = mysql_query ( $verifie_existence_form );
		// ~ $resultat_verifie_existence = mysqli_query($link,$verifie_existence_form);
		$ligne = mysql_fetch_row ( $resultat_verifie_existence );
		// ~ $ligne = mysqli_fetch_row($resultat_verifie_existence);
		$res = $ligne [0];
		
		echo "<table class='principale'>";
		if ($res != 0) {
			$get_form = "SELECT `form` FROM `$db`.`diagnostic` WHERE `nip`=" . $_POST ['nip'];
			$resultat_formclinique = mysql_query ( $get_form );
			// ~ $resultat_formclinique = mysqli_query($link,$get_form);
			while ( $line = mysql_fetch_assoc ( $resultat_formclinique ) ) {
				// ~ while($line = mysqli_fetch_assoc($resultat_formclinique)){
				echo "<tr><td><a href=\"./clinique/page_principale.php?form=" . $line ['form'] . "&action=edit\" > Diagnosis " . $line ['form'] . " </a></td></tr>";
			}
		} elseif ($res == 0) {
			?>
<tr>
	<td>form <?php echo $_POST['formclinique']; ?> not found in diagnostic</td>
</tr><?php
		}
		echo "</table>";
		
		// recherche par nip sur fiche_activite
		$verifie_existence_form = "SELECT COUNT(*) > 0 FROM `$db`.`fiche_activite` WHERE `nip`=" . $_POST ['nip'];
		$resultat_verifie_existence = mysql_query ( $verifie_existence_form );
		// ~ $resultat_verifie_existence = mysqli_query($link,$verifie_existence_form);
		$ligne = mysql_fetch_row ( $resultat_verifie_existence );
		// ~ $ligne = mysqli_fetch_row($resultat_verifie_existence);
		$res = $ligne [0];
		
		echo "<table class='principale'>";
		if ($res != 0) {
			$get_index = "SELECT * FROM `$db`.`fiche_activite` WHERE `nip`=" . $_POST ['nip'];
			$resultat_formfiche_activite = mysql_query ( $get_index );
			// ~ $resultat_formfiche_activite = mysqli_query($link,$get_index);
			while ( $line = mysql_fetch_assoc ( $resultat_formfiche_activite ) ) {
				// ~ while($line = mysqli_fetch_assoc($resultat_formfiche_activite)){
				echo "<tr><td><a href=\"./clinique/fiche_activite.php?index=" . $line ['index'] . "&action=edit\" > Activity form " . $line ['index'] . " </a></td></tr>";
			}
		} elseif ($res == 0) {
			?>
<tr>
	<td>form <?php echo $_POST['formclinique']; ?> not found in activity forms</td>
</tr><?php
		}
		echo "</table>";
	} else {
		
		if (isset ( $_GET ['form'] ) and isset ( $_GET ['table'] )) {
			$form = $_GET ['form'];
			$table = $_GET ['table'];
			require_once ("./no_save.php");
		}
		
		$jointure = 'non';
		if (isset ( $_GET ['reset'] ) and $_GET ['reset'] == 'true') {
			unset ( $_SESSION ['code1'] );
			unset ( $_SESSION ['code2'] );
			unset ( $_SESSION ['code3'] );
			unset ( $_SESSION ['code4'] );
			unset ( $_SESSION ['code5'] );
			unset ( $_SESSION ['codeexterne'] );
			unset ( $_SESSION ['gene'] );
			unset ( $_SESSION ['alias'] );
			
			unset ( $_SESSION ['statut'] );
			unset ( $_SESSION ['validation'] );
			unset ( $_SESSION ['user'] );
			
			unset ( $_SESSION ['diagnostic_affection'] );
			
			unset ( $_SESSION ['techniquescreening'] );
			unset ( $_SESSION ['mutated'] );
			
			unset ( $_SESSION ['techniquewholegenomeanalysis'] );
			
			$requete_vide = 'oui';
		} else {
			
			$requete_vide = 'oui';
			if (isset ( $_POST ['form'] )) {
				$_SESSION ['form'] = $_POST ['form'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['code1'] )) {
				$_SESSION ['code1'] = $_POST ['code1'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['code2'] )) {
				$_SESSION ['code2'] = $_POST ['code2'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['code3'] )) {
				$_SESSION ['code3'] = $_POST ['code3'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['code4'] )) {
				$_SESSION ['code4'] = $_POST ['code4'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['code5'] )) {
				$_SESSION ['code5'] = $_POST ['code5'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['codeexterne'] )) {
				$_SESSION ['codeexterne'] = $_POST ['codeexterne'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['gene'] )) {
				$_SESSION ['gene'] = $_POST ['gene'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['alias'] )) {
				$_SESSION ['alias'] = $_POST ['alias'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['statut'] )) {
				$_SESSION ['statut'] = $_POST ['statut'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['validation'] )) {
				$_SESSION ['validation'] = $_POST ['validation'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['center'] )) {
				$_SESSION ['center'] = $_POST ['center'];
				$requete_vide = 'non';
			}
			if (isset ( $_POST ['user'] )) {
				$_SESSION ['user'] = $_POST ['user'];
				$requete_vide = 'non';
			}
			
			if (isset ( $_POST ['diagnostic_affection'] ) and $_POST ['diagnostic_affection'] != '') {
				$_SESSION ['diagnostic_affection'] = $_POST ['diagnostic_affection'];
				$jointure = 'oui';
				$requete_vide = 'non';
				$table_jointure = 'diagnostic';
			} else if (isset ( $_SESSION ['diagnostic_affection'] ) and $_SESSION ['diagnostic_affection'] != '') {
				$jointure = 'oui';
				$requete_vide = 'non';
				$table_jointure = 'diagnostic';
			}
			
			if (isset ( $_POST ['techniquescreening'] )) {
				$_SESSION ['techniquescreening'] = $_POST ['techniquescreening'];
				if ($_POST ['techniquescreening'] != '') {
					$jointure = 'oui';
					$requete_vide = 'non';
					$table_jointure = 'screening';
				}
			} else if (isset ( $_SESSION ['techniquescreening'] ) and $_SESSION ['techniquescreening'] != '') {
				$jointure = 'oui';
				$requete_vide = 'non';
				$table_jointure = 'screening';
			}
			
			if (isset ( $_POST ['mutated'] )) {
				$_SESSION ['mutated'] = $_POST ['mutated'];
				if ($_POST ['mutated'] != '') {
					$jointure = 'oui';
					$requete_vide = 'non';
					$table_jointure = 'screening';
				}
			} else if (isset ( $_SESSION ['mutated'] ) and $_SESSION ['mutated'] != '') {
				$jointure = 'oui';
				$requete_vide = 'non';
				$table_jointure = 'screening';
			}
			
			if (isset ( $_POST ['techniquewholegenomeanalysis'] )) {
				$_SESSION ['techniquewholegenomeanalysis'] = $_POST ['techniquewholegenomeanalysis'];
				if ($_POST ['techniquewholegenomeanalysis'] != '') {
					$jointure = 'oui';
					$requete_vide = 'non';
					$table_jointure = 'wholegenomeanalysis';
				}
			} else if (isset ( $_SESSION ['techniquewholegenomeanalysis'] ) and $_SESSION ['techniquewholegenomeanalysis'] != '') {
				$jointure = 'oui';
				$requete_vide = 'non';
				$table_jointure = 'wholegenomeanalysis';
			}
		}
		
		?><form action="start.php" method='post'>
	<table class='principale' style='background-color: #FFFCE5;'>


		<tr>
			<td><b>Quick search :</b></td>
			<td>By ID :</td>
			<td style='border-right: 1px solid grey;'><input type="text"
				maxlength="3" size="3" name="code1" id="code1"
				value="<?php if(isset($_SESSION['code1']) and $_SESSION['code1'] != ''){ echo $_SESSION['code1']; $requete_vide='non';} ?>" />-
				<input type="text" maxlength="3" size="3" name="code2" id="code2"
				value="<?php if(isset($_SESSION['code2']) and $_SESSION['code2'] != ''){ echo $_SESSION['code2']; $requete_vide='non';} ?>" />-
				<input type="text" maxlength="3" size="3" name="code3" id="code3"
				value="<?php if(isset($_SESSION['code3']) and $_SESSION['code3'] != ''){ echo $_SESSION['code3']; $requete_vide='non';} ?>" />-
				<input type="text" maxlength="4" size="4" name="code4" id="code4"
				value="<?php if(isset($_SESSION['code4']) and $_SESSION['code4'] != ''){ echo $_SESSION['code4']; $requete_vide='non';} ?>" />-
				<input type="text" maxlength="5" size="5" name="code5" id="code5"
				value="<?php if(isset($_SESSION['code5']) and $_SESSION['code5'] != ''){ echo $_SESSION['code5']; $requete_vide='non';} ?>" />
			</td>
		

		<?php	//~ --------------------------------------------------------------------------------- Recherche globale -------------------------------------------------------------------- ?>
		<td></td>
			<td>By diagnosis form number : <input type="text"
				style="width: 100px;" name="formclinique" /><br /></td>
		
		
		<tr>
			<td></td>
			<td></td>
			<td style='border-right: 1px solid grey;'><br /></td>
			<td></td>
			<td>By NIP number : <input type="text" style="width: 100px;"
				name="nip" /></td>
		</tr>
		<tr></tr>

		<tr>
			<td></td>
			<td>Gene :</td>
			<td style='border-right: 1px solid grey;'><input type="text"
				style="width: 100px;" name="gene" id="gene"
				value="<?php if(isset($_SESSION['gene']) and $_SESSION['gene'] != ''){ echo $_SESSION['gene']; $requete_vide='non';} ?>" />
				(ex: SPAST, KIF1A)<br />
		
		
		<tr>
			<td></td>
			<td>Alias :</td>
			<td style='border-right: 1px solid grey;'><input type="text"
				style="width: 100px;" name="alias" id="alias"
				value="<?php if(isset($_SESSION['alias']) and $_SESSION['alias'] != ''){ echo $_SESSION['alias']; $requete_vide='non';} ?>" />
				(ex: SPG4, SPG30, %SPG%)<br /></td>
		</tr>
		<tr>
			<td></td>
			<td>Status :</td>
			<td style='border-right: 1px solid grey;'><select name="statut"
				id="statut">
					<option></option>
					<option value='ongoing'
						<?php if(isset($_SESSION['statut']) and $_SESSION['statut'] == 'ongoing'){ echo "selected='selected'"; $requete_vide='non';} ?>>
						ongoing</option>
					<option value='done'
						<?php if(isset($_SESSION['statut']) and $_SESSION['statut'] == 'done'){ echo "selected='selected'"; $requete_vide='non'; } ?>>
						done</option>
					<option value='to do'
						<?php if(isset($_SESSION['statut']) and $_SESSION['statut'] == 'to do'){ echo "selected='selected'"; $requete_vide='non';} ?>>
						to do</option>
			</select></td>
		</tr>

		<tr>
			<td></td>
			<td>Validation :</td>
			<td style='border-right: 1px solid grey;'><select name="validation"
				id="validation">
					<option></option>
					<option value='yes'
						<?php if(isset($_SESSION['validation']) and $_SESSION['validation'] == 'yes'){ echo "selected='selected'"; $requete_vide='non';} ?>>
						yes</option>
					<option value='no'
						<?php if(isset($_SESSION['validation']) and $_SESSION['validation'] == 'no'){ echo "selected='selected'"; $requete_vide='non';} ?>>
						no</option>
			</select></td>
		</tr>

		<tr>
			<td></td>
			<td>User :</td>
			<td style='border-right: 1px solid grey;'><input type="text"
				style="width: 100px;" name="user" id="user"
				value="<?php if(isset($_SESSION['user']) and $_SESSION['user'] != ''){echo strtoupper($_SESSION['user']); $requete_vide='non';} ?>" /></td>
		</tr>


		<?php //~------------------------------------------------------------------------------------------ Recherche par table -------------------------------------------------------------------------------------------------------------?>
		
		<tr>
			<td colspan=6><hr /></td>
		</tr>
		<tr>
			<td><b> Diagnosis :</b></td>
			<td>Affection :</td>
			<td><select name="diagnostic_affection" id="diagnostic_affection">
					<option></option>
					<option value='spastic_parapesis : definitely_affected'
						<?php if(isset($_SESSION['diagnostic_affection']) and $_SESSION['diagnostic_affection'] == 'spastic_parapesis : definitely_affected'){ echo "selected='selected'"; $requete_vide='non';} ?>>spastic_parapesis
						: definitely_affected</option>
					<option value='spastic_parapesis : probably_affected'
						<?php if(isset($_SESSION['diagnostic_affection']) and $_SESSION['diagnostic_affection'] == 'spastic_parapesis : probably_affected'){ echo "selected='selected'"; $requete_vide='non';} ?>>spastic_parapesis
						: probably_affected</option>
					<option value='spastic_parapesis : possibly_affected'
						<?php if(isset($_SESSION['diagnostic_affection']) and $_SESSION['diagnostic_affection'] == 'spastic_parapesis : possibly_affected'){ echo "selected='selected'"; $requete_vide='non';} ?>>spastic_parapesis
						: possibly_affected</option>
					<option></option>
					<option value='cerebellar_ataxia : definitely_affected'
						<?php if(isset($_SESSION['diagnostic_affection']) and $_SESSION['diagnostic_affection'] == 'cerebellar_ataxia : definitely_affected'){ echo "selected='selected'"; $requete_vide='non';} ?>>cerebellar_ataxia
						: definitely_affected</option>
					<option value='cerebellar_ataxia : probably_affected'
						<?php if(isset($_SESSION['diagnostic_affection']) and $_SESSION['diagnostic_affection'] == 'cerebellar_ataxia : probably_affected'){ echo "selected='selected'"; $requete_vide='non';} ?>>cerebellar_ataxia
						: probably_affected</option>
					<option value='cerebellar_ataxia : possibly_affected'
						<?php if(isset($_SESSION['diagnostic_affection']) and $_SESSION['diagnostic_affection'] == 'cerebellar_ataxia : possibly_affected'){ echo "selected='selected'"; $requete_vide='non';} ?>>cerebellar_ataxia
						: possibly_affected</option>
			</select></td>
		</tr>

		<tr>
			<td colspan=6><hr /></td>
		</tr>
		<tr>
			<td><b> Screening :</b></td>
			<td>Conclusion :</td>
			<td><select name="mutated" id="mutated">
					<option></option>
					<option value='mutated'
						<?php if(isset($_SESSION['mutated']) and $_SESSION['mutated'] == 'mutated'){ echo "selected='selected'"; $requete_vide='non';} ?>>
						mutated</option>
					<option value='not mutated'
						<?php if(isset($_SESSION['mutated']) and $_SESSION['mutated'] == 'not mutated'){ echo "selected='selected'"; $requete_vide='non';} ?>>
						not mutated</option>
					<option value='healthy carrier'
						<?php if(isset($_SESSION['mutated']) and $_SESSION['mutated'] == 'healthy carrier'){ echo "selected='selected'"; $requete_vide='non';} ?>>
						healthy carrier</option>
			</select></td>
		</tr>
		<tr>
			<td></td>
			<td>Technique :</td>
			<td><input type="text" style="width: 100px;"
				name="techniquescreening" id="techniquescreening"
				value="<?php if(isset($_SESSION['techniquescreening']) and $_SESSION['techniquescreening'] != ''){ echo $_SESSION['techniquescreening']; $requete_vide='non';} ?>" /></td>
		</tr>
		</tr>


		<tr>
			<td colspan=6><hr /></td>
		</tr>
		<tr>
			<td><b> Whole genome analysis :</b></td>
			<td>Technique :</td>
			<td><input type="text" style="width: 100px;"
				name="techniquewholegenomeanalysis"
				id="techniquewholegenomeanalysis"
				value="<?php if(isset($_SESSION['techniquewholegenomeanalysis']) and $_SESSION['techniquewholegenomeanalysis'] != ''){ echo $_SESSION['techniquewholegenomeanalysis']; $requete_vide='non';} ?>" /></td>
		</tr>


		<tr>
			<td><input type='button'
				onClick="location.href='./start.php?reset=true'" value='reset'><input
				type='submit' value='submit' /></td>
		</tr>
	</table>
</form>


<?php
		?><p style='margin-left: 50px;'>
	<img src='./images/carre_vert.jpg' height='10' /><i> validated</i> <img
		src='./images/carre_rouge.jpg' height='10' /><i> not validated</i>
</p><?php
		
		$liste_tables = Array (
				'diagnostic',
				'linkage',
				'screening',
				'wholegenomeanalysis' 
		);
		$hash_table_champs = Array (
				'diagnostic' => Array (
						'form',
						'followup',
						'nip',
						'birthdate',
						'proband',
						'date',
						'user' 
				),
				'linkage' => Array (
						'gene',
						'alias',
						'linkage',
						'statut',
						'afaire',
						'date',
						'user' 
				),
				'screening' => Array (
						'gene',
						'alias',
						'diagnosticscreening',
						'techniquescreening',
						'statut',
						'afaire',
						'date',
						'user' 
				),
				'wholegenomeanalysis' => Array (
						'diagnosticwholegenomeanalysis',
						'techniquewholegenomeanalysis',
						'commentairetechnique',
						'statut',
						'afaire',
						'date',
						'user' 
				) 
		);
		
		// ~ -------------------------------------------------------------------------- construction de la requete -----------------------------------------------------------------------------
		foreach ( $liste_tables as $table ) {
			
			// ~ -------------------------------------------------------------------------- partie commmune -----------------------------------------------------------------------------
			$requete_form_numbers = "SELECT DISTINCT `$table`.*";
			$requete = '';
			
			// ~ -------------------------------------------------------------------------- jointure -----------------------------------------------------------------------------
			if ($jointure == 'oui' and $table_jointure != $table) {
				$requete .= "
				FROM  `$table` , `$table_jointure`
				WHERE";
				
				$requete_jointure = '';
				// ~ -------------------------------------------------------------------------- table clinique -----------------------------------------------------------------------------
				if ($table_jointure == 'diagnostic') {
					if (isset ( $_SESSION ['code5'] ) and $_SESSION ['code5'] != '') {
						$requete_jointure .= " AND `code5` LIKE '" . $_SESSION ['code5'] . "'";
					}
					if (isset ( $_SESSION ['codegenethon'] ) and $_SESSION ['codegenethon'] != '') {
						$requete_jointure .= " AND `codegenethon` LIKE '" . $_SESSION ['codegenethon'] . "'";
					}
					if (isset ( $_SESSION ['diagnostic_affection'] ) and $_SESSION ['diagnostic_affection'] != '') {
						$table_diagnostic = explode ( ' : ', $_SESSION ['diagnostic_affection'] );
						$requete_jointure .= " AND `diagnostic_" . $table_diagnostic [0] . "_affection` LIKE '" . $table_diagnostic [1] . "'";
					}
					if (isset ( $_SESSION ['nip'] ) and $_SESSION ['nip'] != '') {
						$requete_jointure .= " AND `nip` LIKE '" . $_SESSION ['nip'] . "'";
					}
					$orderby = " ORDER BY `code4`,`code5`,`date`";
				}
				
				// ~ -------------------------------------------------------------------------- table linkage -----------------------------------------------------------------------------
				if ($table_jointure == 'linkage') {
					if (isset ( $_SESSION ['gene'] ) and $_SESSION ['gene'] != '') {
						$requete_jointure .= " AND `gene` LIKE '" . $_SESSION ['gene'] . "'";
					}
					if (isset ( $_SESSION ['alias'] ) and $_SESSION ['alias'] != '') {
						$requete_jointure .= " AND `alias` LIKE '" . $_SESSION ['alias'] . "'";
					}
					if (isset ( $_SESSION ['statut'] ) and $_SESSION ['statut'] != '') {
						$requete_jointure .= " AND `statut` LIKE '" . $_SESSION ['statut'] . "'";
					}
					$orderby = " ORDER BY `code4`,`date`";
				}
				
				// ~ -------------------------------------------------------------------------- screening -----------------------------------------------------------------------------
				if ($table_jointure == 'screening') {
					if (isset ( $_SESSION ['gene'] ) and $_SESSION ['gene'] != '') {
						$requete_jointure .= " AND `gene` LIKE '" . $_SESSION ['gene'] . "'";
					}
					if (isset ( $_SESSION ['alias'] ) and $_SESSION ['alias'] != '') {
						$requete_jointure .= " AND `alias` LIKE '" . $_SESSION ['alias'] . "'";
					}
					if (isset ( $_SESSION ['mutated'] ) and $_SESSION ['mutated'] != '') {
						$requete_jointure .= " AND `diagnosticscreening` LIKE '" . $_SESSION ['mutated'] . "'";
					}
					if (isset ( $_SESSION ['techniquescreening'] ) and $_SESSION ['techniquescreening'] != '') {
						$requete_jointure .= " AND `techniquescreening` LIKE '" . $_SESSION ['techniquescreening'] . "'";
					}
					if (isset ( $_SESSION ['statut'] ) and $_SESSION ['statut'] != '') {
						$requete_jointure .= " AND `statut` LIKE '" . $_SESSION ['statut'] . "'";
					}
					$orderby = " ORDER BY `code4`,`code5`,`date`";
				}
				
				// ~ -------------------------------------------------------------------------- wholegenomeanalysis -----------------------------------------------------------------------------
				if ($table_jointure == 'wholegenomeanalysis') {
					if (isset ( $_SESSION ['gene'] ) and $_SESSION ['gene'] != '') {
						$requete_jointure .= " AND `diagnosticwholegenomeanalysis` LIKE '" . $_SESSION ['gene'] . "'";
					}
					if (isset ( $_SESSION ['alias'] ) and $_SESSION ['alias'] != '') {
						$requete_jointure .= " AND `diagnosticwholegenomeanalysis` LIKE '" . $_SESSION ['alias'] . "'";
					}
					if (isset ( $_SESSION ['techniquewholegenomeanalysis'] ) and $_SESSION ['techniquewholegenomeanalysis'] != '') {
						$requete_jointure .= " AND `techniquewholegenomeanalysis` LIKE '" . $_SESSION ['techniquewholegenomeanalysis'] . "'";
					}
					if (isset ( $_SESSION ['statut'] ) and $_SESSION ['statut'] != '') {
						$requete_jointure .= " AND `statut` LIKE '" . $_SESSION ['statut'] . "'";
					}
					$orderby = " ORDER BY `code4`,`code5`,`date`";
				}
				
				$requete_jointure = ltrim ( $requete_jointure, " AND" );
				$requete .= $requete_jointure;
				$requete .= "
				AND  `$table`.`code1` =  `$table_jointure`.`code1` 
				AND  `$table`.`code2` =  `$table_jointure`.`code2` 
				AND  `$table`.`code3` =  `$table_jointure`.`code3` 
				AND  `$table`.`code4` =  `$table_jointure`.`code4`";
				if (isset ( $_SESSION ['code1'] ) and $_SESSION ['code1'] != '') {
					$requete .= " AND `$table`.`code1` LIKE '" . $_SESSION ['code1'] . "'";
				}
				if (isset ( $_SESSION ['code2'] ) and $_SESSION ['code2'] != '') {
					$requete .= " AND `$table`.`code2` LIKE '" . $_SESSION ['code2'] . "'";
				}
				if (isset ( $_SESSION ['code3'] ) and $_SESSION ['code3'] != '') {
					$requete .= " AND `$table`.`code3` LIKE '" . $_SESSION ['code3'] . "'";
				}
				if (isset ( $_SESSION ['code4'] ) and $_SESSION ['code4'] != '') {
					$requete .= " AND `$table`.`code4` LIKE '" . $_SESSION ['code4'] . "'";
				}
				if (isset ( $_SESSION ['user'] ) and $_SESSION ['user'] != '') {
					$requete .= " AND `$table`.`user` LIKE '" . $_SESSION ['user'] . "'";
				}
				
				if ($table != 'linkage') {
					$requete .= " AND  `$table`.`code5` =  `$table_jointure`.`code5`";
					$orderby = " ORDER BY `code4`,`code5`,`date`";
				} else {
					$orderby = " ORDER BY `code4`,`date`";
				}
			} else {
				$requete_form_numbers .= " FROM `$table` WHERE ";
				if (isset ( $_SESSION ['code1'] ) and $_SESSION ['code1'] != '') {
					$requete .= " AND `code1` LIKE '" . $_SESSION ['code1'] . "'";
				}
				if (isset ( $_SESSION ['code2'] ) and $_SESSION ['code2'] != '') {
					$requete .= " AND `code2` LIKE '" . $_SESSION ['code2'] . "'";
				}
				if (isset ( $_SESSION ['code3'] ) and $_SESSION ['code3'] != '') {
					$requete .= " AND `code3` LIKE '" . $_SESSION ['code3'] . "'";
				}
				if (isset ( $_SESSION ['code4'] ) and $_SESSION ['code4'] != '') {
					$requete .= " AND `code4` LIKE '" . $_SESSION ['code4'] . "'";
				}
				if (isset ( $_SESSION ['user'] ) and $_SESSION ['user'] != '') {
					$requete .= " AND `user` LIKE '" . $_SESSION ['user'] . "'";
				}
				
				// ~ -------------------------------------------------------------------------- table clinique -----------------------------------------------------------------------------
				if ($table == 'diagnostic') {
					if (isset ( $_SESSION ['code5'] ) and $_SESSION ['code5'] != '') {
						$requete .= " AND `code5` LIKE '" . $_SESSION ['code5'] . "'";
					}
					if (isset ( $_SESSION ['codegenethon'] ) and $_SESSION ['codegenethon'] != '') {
						$requete .= " AND `codegenethon` LIKE '" . $_SESSION ['codegenethon'] . "'";
					}
					if (isset ( $_SESSION ['diagnostic_affection'] ) and $_SESSION ['diagnostic_affection'] != '') {
						$table_diagnostic = explode ( ' : ', $_SESSION ['diagnostic_affection'] );
						$requete .= " AND `diagnostic_" . $table_diagnostic [0] . "_affection` LIKE '" . $table_diagnostic [1] . "'";
					}
					if (isset ( $_SESSION ['nip'] ) and $_SESSION ['nip'] != '') {
						$requete .= " AND `nip` LIKE '" . $_SESSION ['nip'] . "'";
					}
					$orderby = " ORDER BY `code4`,`code5`,`date`";
				}
				
				// ~ -------------------------------------------------------------------------- table linkage -----------------------------------------------------------------------------
				if ($table == 'linkage') {
					if (isset ( $_SESSION ['gene'] ) and $_SESSION ['gene'] != '') {
						$requete .= " AND `gene` LIKE '" . $_SESSION ['gene'] . "'";
					}
					if (isset ( $_SESSION ['alias'] ) and $_SESSION ['alias'] != '') {
						$requete .= " AND `alias` LIKE '" . $_SESSION ['alias'] . "'";
					}
					if (isset ( $_SESSION ['statut'] ) and $_SESSION ['statut'] != '') {
						$requete .= " AND `statut` LIKE '" . $_SESSION ['statut'] . "'";
					}
					$orderby = " ORDER BY `code4`,`date`";
				}
				
				// ~ -------------------------------------------------------------------------- screening -----------------------------------------------------------------------------
				if ($table == 'screening') {
					if (isset ( $_SESSION ['code5'] ) and $_SESSION ['code5'] != '') {
						$requete .= " AND `code5` LIKE '" . $_SESSION ['code5'] . "'";
					}
					if (isset ( $_SESSION ['gene'] ) and $_SESSION ['gene'] != '') {
						$requete .= " AND `gene` LIKE '" . $_SESSION ['gene'] . "'";
					}
					if (isset ( $_SESSION ['alias'] ) and $_SESSION ['alias'] != '') {
						$requete .= " AND `alias` LIKE '" . $_SESSION ['alias'] . "'";
					}
					if (isset ( $_SESSION ['mutated'] ) and $_SESSION ['mutated'] != '') {
						$requete .= " AND `diagnosticscreening` LIKE '" . $_SESSION ['mutated'] . "'";
					}
					if (isset ( $_SESSION ['techniquescreening'] ) and $_SESSION ['techniquescreening'] != '') {
						$requete .= " AND `techniquescreening` LIKE '" . $_SESSION ['techniquescreening'] . "'";
					}
					if (isset ( $_SESSION ['statut'] ) and $_SESSION ['statut'] != '') {
						$requete .= " AND `statut` LIKE '" . $_SESSION ['statut'] . "'";
					}
					$orderby = " ORDER BY `code4`,`code5`,`date`";
				}
				
				// ~ -------------------------------------------------------------------------- wholegenomeanalysis -----------------------------------------------------------------------------
				if ($table == 'wholegenomeanalysis') {
					if (isset ( $_SESSION ['code5'] ) and $_SESSION ['code5'] != '') {
						$requete .= " AND `code5` LIKE '" . $_SESSION ['code5'] . "'";
					}
					if (isset ( $_SESSION ['gene'] ) and $_SESSION ['gene'] != '') {
						$requete .= " AND `diagnosticwholegenomeanalysis` LIKE '" . $_SESSION ['gene'] . "'";
					}
					if (isset ( $_SESSION ['alias'] ) and $_SESSION ['alias'] != '') {
						$requete .= " AND `diagnosticwholegenomeanalysis` LIKE '" . $_SESSION ['alias'] . "'";
					}
					if (isset ( $_SESSION ['techniquewholegenomeanalysis'] ) and $_SESSION ['techniquewholegenomeanalysis'] != '') {
						$requete .= " AND `techniquewholegenomeanalysis` LIKE '" . $_SESSION ['techniquewholegenomeanalysis'] . "'";
					}
					if (isset ( $_SESSION ['statut'] ) and $_SESSION ['statut'] != '') {
						$requete .= " AND `statut` LIKE '" . $_SESSION ['statut'] . "'";
					}
					$orderby = " ORDER BY `code4`,`code5`,`date`";
				}
			}
			
			// ~ -------------------------------------------------------------------------- Requete et affichage -----------------------------------------------------------------------------
			$requete = ltrim ( $requete, " AND" );
			$requete_finale = $requete_form_numbers . $requete . $orderby;
			
			if ($requete_vide == 'non' and ! strpos ( $requete_finale, 'WHERE  ORDER' )) { // si un formulaire a été envoyé
			                                                                        // ~ echo $rsequete_finale;
				$result_requete_finale = mysql_query ( $requete_finale ) or die ( "Error $table data : " . mysql_error () );
				// ~ $result_requete_finale = mysqli_query($link,$requete_finale) or die("Error $table data : " . mysql_error());
				
				if ($table == 'diagnostic' and $_SESSION ['acces_clinique'] >= 1 or preg_match ( "/linkage|screening|wholegenomeanalysis/", $table ) and $_SESSION ['acces_genetique'] >= 1) {
					
					// ~ affichage debut table ---------------
					echo "<table class='principale'>";
					echo "<tr><td><b><img src='./images/clinique.jpg' height='20' width='20'> " . ucfirst ( $table );
					if ($jointure == 'oui' and $table_jointure != $table) {
						echo " (join)";
					}
					echo "</b><br /><br /></td></tr>";
					echo "<tr/><td></td><td><b>" . implode ( $hash_table_champs [$table], "</b></td><td><b>" ) . "</b></td></tr>";
					
					// ~ affichage ligne ---------------
					while ( $ligne = mysql_fetch_assoc ( $result_requete_finale ) ) {
						// ~ while($ligne = mysqli_fetch_assoc($result_requete_finale)) {
						$individu = $ligne ['code1'] . '-' . $ligne ['code2'] . '-' . $ligne ['code3'] . '-' . $ligne ['code4'];
						if (isset ( $ligne ['code5'] )) {
							$individu .= '-' . $ligne ['code5'];
						}
						$famille = $ligne ['code1'] . '-' . $ligne ['code2'] . '-' . $ligne ['code3'] . '-' . $ligne ['code4'];
						
						// ~ supprimer / modifier ---------------
						echo "<tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">";
						if ($table == 'diagnostic' and $_SESSION ['acces_clinique'] > 1) { // modification
							echo "<td class='lisible' style='width:600px;'>
							<a href='./clinique/page_principale.php?form=" . $ligne ['form'] . "'><img src='" . $_SESSION ['pathway'] . "/images/modifier.png' height='15' /></a> 
							<img src='" . $_SESSION ['pathway'] . "/images/supprimer.png' height='15' onclick=\"confirmer_suppression('" . $ligne ['form'] . "', '$table','$individu')\" />&nbsp;";
						} else if (preg_match ( "/linkage|screening|wholegenomeanalysis/", $table ) and $_SESSION ['acces_genetique'] >= 2) {
							echo "<td class='lisible' style='width:600px;'><a href='./genetiquespataxeurospa/$table.php?action=edit&ind=" . $individu . "&index=" . $ligne ['index'] . "&table=$table' ><img src='" . $_SESSION ['pathway'] . "/images/modifier.png' height='15' /></a>&nbsp;";
							echo "<img src='" . $_SESSION ['pathway'] . "/images/supprimer.png' height='15' onclick=\"confirmer_suppression('" . $ligne ['index'] . "', '$table','$individu')\" />&nbsp;";
						} else {
							echo "<td class='lisible' style='width:600px;'>";
							echo "<img src='" . $_SESSION ['pathway'] . "/images/modifier_grise.png' height='15' />
							<img src='" . $_SESSION ['pathway'] . "/images/supprimer_grise.png' height='15' />&nbsp;";
						}
						
						// ~ arbre ---------------
						if (file_exists ( './uploads/pedigree/' . $famille . '.ppt' )) {
							echo "<a target=_blank href='./uploads/pedigree/$famille.ppt'><img src='" . $_SESSION ['pathway'] . "/images/logo_arbre.png' height='15' /> </a>";
						} elseif (file_exists ( './uploads/pedigree/' . $famille . '.pptx' )) {
							echo "<a target=_blank href='./uploads/pedigree/$famille.pptx'><img src='" . $_SESSION ['pathway'] . "/images/logo_arbre.png' height='15' /> </a>";
						} elseif (file_exists ( './uploads/pedigree/' . $famille . '.jpg' )) {
							echo "<a target=_blank href='./uploads/pedigree/$famille.jpg'><img src='" . $_SESSION ['pathway'] . "/images/logo_arbre.png' height='15' /> </a>";
						} else {
							if ($_SESSION ['acces_clinique'] > 1) {
								echo "<img src='" . $_SESSION ['pathway'] . "/images/logo_arbre_gris.png' height='15' onclick=\"surdevouloiruploader()\" /> ";
							} else {
								echo "<img src='" . $_SESSION ['pathway'] . "/images/logo_arbre_gris.png' height='15' /> ";
							}
						}
						
						// ~ cas par cas ---------------
						if ($table == 'diagnostic' and $_SESSION ['acces_clinique'] >= 1) {
							$diag_string = '';
							if (isset ( $ligne ['diagnostic_spastic_parapesis_type'] ) and $ligne ['diagnostic_spastic_parapesis_type'] != '' or isset ( $ligne ['diagnostic_spastic_parapesis_form'] ) and $ligne ['diagnostic_spastic_parapesis_form'] != '' or isset ( $ligne ['diagnostic_spastic_parapesis_affection'] ) and $ligne ['diagnostic_spastic_parapesis_affection'] != '') {
								$diag_string .= "PS : " . str_replace ( '_', ' ', $ligne ['diagnostic_spastic_parapesis_affection'] ) . "<br />";
							}
							if (isset ( $ligne ['diagnostic_cerebellar_ataxia_type'] ) and $ligne ['diagnostic_cerebellar_ataxia_type'] != '' or isset ( $ligne ['diagnostic_cerebellar_ataxia_form'] ) and $ligne ['diagnostic_cerebellar_ataxia_form'] != '' or isset ( $ligne ['diagnostic_cerebellar_ataxia_affection'] ) and $ligne ['diagnostic_cerebellar_ataxia_affection'] != '') {
								$diag_string .= "Ataxia : " . str_replace ( '_', ' ', $ligne ['diagnostic_cerebellar_ataxia_affection'] );
							}
							echo "<a href='./clinique/page_principale.php?form=" . $ligne ['form'] . "'> " . $individu . "</a></td>";
						} else if ($table == 'linkage' and $_SESSION ['acces_genetique'] >= 1) {
							$individu = $ligne ['code1'] . '-' . $ligne ['code2'] . '-' . $ligne ['code3'] . '-' . $ligne ['code4'];
							echo "&nbsp;<a href='./genetiquespataxeurospa/$table.php?action=edit&ind=" . $individu . "&index=" . $ligne ['index'] . "&table=$table' >" . $individu . " </a>";
						} else if ($table == 'screening' and $_SESSION ['acces_genetique'] >= 1) {
							echo "&nbsp;<a href='./genetiquespataxeurospa/$table.php?action=edit&ind=" . $individu . "&index=" . $ligne ['index'] . "&table=$table' >" . $individu . " </a>";
						} else if ($table == 'wholegenomeanalysis' and $_SESSION ['acces_genetique'] >= 1) {
							echo "&nbsp;<a href='./genetiquespataxeurospa/$table.php?action=edit&ind=" . $individu . "&index=" . $ligne ['index'] . "&table=$table' >" . $individu . " </a>";
						} else {
							echo "<important>You don't have the permission to acces to this data</important>";
						}
						
						// ~ affichage reste ligne ---------------
						
						foreach ( $hash_table_champs [$table] as $champ_a_afficher ) {
							echo "<td class='lisible'>" . $ligne [$champ_a_afficher] . "</td>";
						}
						
						echo "<td class='lisible' style='width:100px;'>";
						if (isset ( $ligne ['validation'] ) and $ligne ['validation'] == 'yes') {
							echo "<img src='" . $_SESSION ['pathway'] . "/images/carre_vert.jpg' height='12' />";
						} else if (isset ( $ligne ['validation'] ) and $ligne ['validation'] == 'no') {
							echo "<img src='" . $_SESSION ['pathway'] . "/images/carre_rouge.jpg' height='12' />";
						}
					}
					
					if ($table == 'diagnostic' and $_SESSION ['acces_clinique'] >= 2) {
						echo "<tr><td onmouseover=\"this.bgColor='white'\"><input type='button' onclick=\"surdecreernouveauformulaire('" . $_SESSION ['pathway'] . "')\" value='Insert diagnosis form'></td></tr>";
					} else if (preg_match ( "/linkage|screening|wholegenomeanalysis/", $table) and $_SESSION['acces_genetique'] >= 2){
						echo "<tr><td onmouseover=\"this.bgColor='white'\"><input type='button' value='Insert $table form' onclick='window.location.href=\"./genetiquespataxeurospa/$table.php?action=insert&table=$table\"' /></td></tr>";
					}

					echo "</table>\n";
					
				}

			}
		
		}

	}

	require_once("./requetes/recup_niveau.php");
	



}
include('./footer.php');

?>
